/**
 * layui的初始化dom和事件封装
 */
$(function() {
	layui.use([ 'form', 'layer', 'laydate', 'table', 'laytpl' ], function() {
		var form = layui.form, layer = parent.layer === undefined ? layui.layer : top.layer, $ = layui.jquery, laydate = layui.laydate, laytpl = layui.laytpl, table = layui.table;
		var FORM_STATUS={   // 防止表单重复提交变量
				"POSTING":"posting",   // 表单正在提交时点击提交按钮不做反应;
				"POSTED":"posted",    // //要求只能提交一次时;
				"NORMAL":"normal"  // 提交完成，复原状态位
		}
		// 下拉框 支持自定义 默认选择
		$("select").each(function(index, domEle) {
			var $this = $(this);
			var v = $(this).attr("v");
			if (v != undefined && v != null && $.trim(v) != "") { // 如果不为空，那么执行选中初始化
				$this.val(v);
				form.render('select'); // siblings 筛选同辈元素
				$this.siblings("div.layui-form-select").find('dl').find('dd[lay-value="' + v + '"]').click();
			}
		});
		// 增加刷新样式初始化
		$(document).on("click", ".boot-reload", function() {
			self.location.reload();  //这里调整一下  改为重新
		});

		// =========================3封装的layui样式的事件触发==============================================
		/* 弹出层封装 */
		$(document).on("click", ".boot-layer", function() {
			var $this = $(this);
			var bootTitle = $this.attr("boot-title");
			var bootUrl = $this.attr("boot-url");
			var bootWidth = $this.attr("boot-width");
			var bootHeight = $this.attr("boot-height");
			var endStr = $this.attr("end");
			var end =window["endStr"];
			$.lay.openIframe(bootTitle, bootUrl, bootWidth, bootHeight, end);
		});
		/* ajax请求封装 */
		$(document).on("click", ".boot-ajax", function() {
			var $this = $(this);
			var bootTitle = $this.attr("boot-title");
			var bootUrl = $this.attr("boot-url");
			if ($this.hasClass("boot-batch")) {
				var ids = $.lay.tableGetCheckIds("listTable");
				if (_.isEmpty(ids)) {
					layer.alert("请选中在操作");
					return;
				}
				bootUrl = bootUrl + '?ids=' + ids;
			}
			var bootData = $this.attr("boot-data");
			if (bootData) {
				var obj;
				try {
					console.log("'"+bootData+"'");
					obj = $.parseJSON(bootData);
				} catch (e) {
					console.log(e);
					layer.alert("请填写正确的参数要求，例如{name:姓名,age:22}");
					return;
				}
				bootData = $.param(obj);
				if (bootUrl.indexOf("?") != -1) {
					bootUrl = bootUrl + "&" + bootData;
				} else {
					bootUrl = bootUrl + "?" + bootData;
				}
			}
			var successFunction = $this.attr("boot-successFunction");
			$.lay.confirm(bootTitle, bootUrl, successFunction);
		});
		/**
		 * form封装 // 监听提交+防止表单重复提交
		 */
		form.on('submit(boot-form-btn)', function(data) {
			/*
			 * console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
			 * console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
			 * console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
			 */
			var  curform = data.form;
			// 表单正在提交时点击提交按钮不做反应;
			if(curform[0].validform_status===FORM_STATUS.POSTING){ return false;}
			// 要求只能提交一次时;
// if(settings.postonce &&
// curform[0].validform_status===FORM_STATUS.POSTED){return false;}
			var $form = $(curform);
			var action = $form.attr("action");
			var successFunction = $form.attr("boot-successFunction");
			if (_.isEmpty(successFunction)) {
				successFunction = function() { //
				}
			}
			if (_.isEmpty(action)) {
				alert(action + "action 为空");
			} else {
				curform[0].validform_status=FORM_STATUS.POSTING;
				$.ajax({
					url:action,
					data:$form.serializeObject(), 
		           dataType: "json",
		           type: "post",
					success:function(result) {
					layer.msg(result.msg);
					if (result.code == 200) {
						$.lay.closeIframe();
						$.lay.reloadUseIframe();
						successFunction();
					}
				},
				complete : function(XMLHttpRequest, textStatus) { // 请求完成时运行的函数（在请求成功或失败之后均调用，即在
					curform[0].validform_status=FORM_STATUS.NORMAL;  // 提交完成 // 状态改回正常
				}
				});
			}
			return false;// 阻止表单跳转。如果需要表单跳转，去掉这段即可。
		});
		// form 条件搜索并且刷新对应的数据
		$(".boot-search-btn").on("click", function() {
			var $searchForm = $(this).closest("form");
			var layTableId = $searchForm.attr("layTableId");
			if (_.isEmpty(layTableId)) {
				layTableId = "listTable";
			}
			table.reload(layTableId, {
				page : {
					curr : 1
				// 重新从第 1 页开始
				},
				where : $searchForm.serializeObject()
			});
		});

	});
});
